from django.db import models
from django.utils.html import format_html

class AdminUser(models.Model):
    openid = models.CharField('openid', max_length=32)
    nickName = models.CharField('昵称', max_length=10,blank=True,null=True,default=None)
    mobile = models.CharField('手机号', max_length=11)
    created_at = models.DateTimeField()
    qrcode = models.CharField("二维码", max_length=100)
    permission = models.IntegerField('是否可访问', default=0)

    def qrcode_colored(self):
        if self.qrcode == None:
            return format_html('<a href="/quanquaner/wx/qrcode/add/{}/">申请二维码</a>', self.id)
        else:
            return format_html('<span style="color: red;">已申请</span>')
    qrcode_colored.short_description = '二维码'

    class Meta:
        db_table = 'admin_users'
        verbose_name_plural = '登录用户'


class Log(models.Model):
    openid = models.CharField('openid',max_length=32)
    log = models.CharField('操作', max_length=255)
    created_at = models.DateTimeField('操作时间', auto_now_add=True)

    class Meta:
        db_table = 'logs'
        verbose_name_plural = '用户日志'

class LastdayNum(models.Model):
    data_name = models.CharField('数据名称', max_length=32)
    data_num = models.IntegerField('数据')
    date = models.DateField('日期')

    class Meta:
        db_table = 'lastday_user'
        verbose_name_plural = '数据统计'

class qqData(models.Model):
    uid = models.CharField('uuid', max_length=32)
    date = models.DateTimeField('时间')
    salesId = models.CharField('销售ID', max_length=12)
    imei = models.CharField('IMEI', max_length=32)
    phoneBank = models.CharField('手机品牌', max_length=16)
    phoneModel  = models.CharField('手机型号', max_length=16)
    taskStatus = models.IntegerField('换机结果')
    auditStatus = models.IntegerField('审核状态')

    def audit_colored(self):
        if self.auditStatus == 1:
            color_code = 'red'
            audit_code = "未审核"
        elif self.auditStatus == 0:
            color_code = 'green'
            audit_code = "已审核"
        return format_html(
            '<span style="color: {};">{}</span>',
            color_code,
            audit_code,
        )

    audit_colored.short_description = u'审核状态'

    def task_colored(self):
        if self.taskStatus == 0:
            color_code = 'green'
            task_code = "通过"
        elif self.taskStatus == 1:
            color_code = 'yellow'
            task_code = "未达标"
        elif self.taskStatus == 2:
            color_code = 'red'
            task_code = "旧机"
        return format_html(
            '<span style="color: {};">{}</span>',
            color_code,
            task_code,
        )

    task_colored.short_description = u'换机结果'

    class Meta:
        db_table = 'qq_data'
        verbose_name_plural = '同步助手'

class qqDataUploadPage(models.Model):

    class Meta:
        verbose_name_plural = '同步助手数据上传'

class SalsesTop(models.Model):
    userId = models.IntegerField('用户ID')
    mobile = models.CharField('手机号', max_length=11)
    area = models.CharField('位置', max_length=20)
    order_num = models.IntegerField('订单数')
    qh_coin = models.IntegerField('和币')
    created_at = models.DateTimeField(auto_now_add=True)

    def mobile_hide(self):
        return self.mobile[:3] + '****' + self.mobile[7:]

    mobile_hide.short_description = u'手机号'

    class Meta:
        db_table = 'sales_top'
        verbose_name_plural = '销售排行'

# class JiaoShuoShuo(models.Model):
#     column = models.IntegerField('季数')
#     periods = models.IntegerField('期数')
#     title = models.CharField('名称', max_length=32)
#     content = models.FileField(upload_to='jiaoshoushuo/')
#     cover = models.ImageField(upload_to='jiaoshoushuo/', verbose_name='封面')
#     detail = models.ImageField(upload_to='jiaoshoushuo/', verbose_name='详情')
#
#     class Meta:
#         db_table = 'jiaoshoushuo'
#         verbose_name_plural = '教售说'

class BaiduCodeUploadPage(models.Model):

    class Meta:
        verbose_name_plural = '百度推广码上传'

class BaiduCode(models.Model):
    baidu_url = models.CharField('百度URL', max_length=255)
    haokan_url = models.CharField('好看URL', max_length=255)
    user_name = models.CharField('店员姓名', max_length=20)
    userId = models.IntegerField('用户', null=True, blank=True)
    mobile = models.CharField('手机号', max_length=11)
    click = models.IntegerField('点击量', default=0)
    # team = models.ForeignKey('Customs', on_delete=models.SET_NULL, null=True, to_field='id', related_name='team', verbose_name='商户')
    # proxy = models.ForeignKey('Customs', on_delete=models.SET_NULL, null=True, to_field='id', related_name='proxy', verbose_name='代理')
    # admin_user = models.ForeignKey('Customs', on_delete=models.SET_NULL, null=True, to_field='id', related_name='admin', verbose_name='推广经理')
    status = models.IntegerField(default=1)

    def status_colored(self):
        if self.status == 0:
            st_color = 'red'
            st_str = '未启用'
        elif self.status == 1:
            st_color = 'dodgerblue'
            st_str = '未使用'
        elif self.status == 2:
            st_color = 'green'
            st_str = '正常'
        return format_html('<span style="color: {};">{}</span>', st_color, st_str)
    status_colored.short_description = '状态'

    def getUserInfo(self):
        if self.mobile == None or self.mobile == '':
            return ''
        else:
            return format_html('<a href="/quanquaner/getUserInfo/{}/"><span style="color: green;">更新用户信息</span></a>', self.id)
    getUserInfo.short_description = '操作'

    def get_order_sum(self):
        #order_num =  BaiduOrderData.objects.filter(qudao_num=self.qudao_num).aggregate(num_order=models.Sum('login_user'))
        #return order_num['num_order']
        return ""
    get_order_sum.short_description = '订单汇总'

    def __str__(self):
        return self.user_name

    class Meta:
        db_table = 'bq_baidu_code'
        verbose_name_plural = '百度推广码'
        permissions = (
            ("baidu_code", u"百度推广码"),
        )

class Material(models.Model):
    name = models.CharField('名称', max_length=20)
    media_id = models.CharField(max_length=50, blank=True)
    media_url = models.CharField(max_length=255, blank=True)
    media = models.ImageField('素材')
    status = models.IntegerField('状态', blank=True)

    def media_upload(self):
        if self.media_id.strip() == '' or self.media_id == None:
            return format_html('<a href="/quanquaner/wx/media/add/{}/">上传素材</a>', self.id)
        else:
            return format_html('<span style="color: red;">已上传</span>')
    media_upload.short_description = '操作'

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'materials'
        verbose_name_plural = u'素材管理'

class News(models.Model):
    title = models.CharField('标题', max_length=32)
    description = models.CharField('描述', max_length=255)
    media = models.ForeignKey('Material', on_delete=models.SET_NULL, null=True, verbose_name='素材')
    Url = models.CharField('链接', max_length=255)

    class Meta:
        db_table = 'news'
        verbose_name_plural = u'图文消息'

class BaiduDataUploadPage(models.Model):

    class Meta:
        verbose_name_plural = '百度推广数据上传'
        permissions = (
            ("baidu_data_upload", u"百度推广数据上传"),
        )

class BaiduOrderData(models.Model):
    baidu = models.ForeignKey('BaiduCode', on_delete=models.SET_NULL, db_column='baidu_code', null=True, verbose_name='店员')
    app_name = models.CharField('应用名称', max_length=12)
    valid_num = models.IntegerField('有效', default=0)
    yuzhuang_valid_num = models.IntegerField('预装有效', default=0)
    phone_type = models.IntegerField('手机型号')
    team = models.CharField('商户', max_length=50)
    proxy = models.CharField('代理', max_length=50)
    admin_user = models.CharField('推广经理', max_length=20)
    date = models.DateField('作业日期')

    class Meta:
        db_table = 'baidu_order_data'
        verbose_name_plural = u'百度推广数据'
        permissions = (
            ("baidu_data", u"百度推广数据"),
        )

class Customs(models.Model):
    custom_id = models.IntegerField(unique=True)
    name = models.CharField('名称', max_length=50)
    role = models.IntegerField('角色')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'customs'
        verbose_name_plural = u'客户管理'

class TmallCode(models.Model):
    mobile= models.CharField("手机号", max_length=11)
    user = models.CharField("联系人", max_length=20)
    store = models.CharField("门店", max_length=120)
    store_sn = models.CharField("门店编码", max_length=7)
    pid = models.CharField(max_length=255)
    store_img = models.CharField("门头照", max_length=255)
    valid_number = models.CharField("营业执照", max_length=50)
    province = models.CharField("省份", max_length=20)
    city = models.CharField("市", max_length=20)
    district = models.CharField("县区", max_length=20)
    address = models.CharField("地址", max_length=255)
    status = models.IntegerField("状态", default=0)

    class Meta:
        db_table = 'tmall_code'
        verbose_name_plural = u'天猫门店二维码'
        permissions = (
            ("tmall", u"天猫二维码"),
        )

class TmallStore(models.Model):
    mobile= models.CharField("手机号", max_length=20)
    user = models.CharField("联系人", max_length=20)
    store = models.CharField("门店", max_length=120)
    valid_number = models.CharField("营业执照", max_length=50)
    province = models.CharField("省份", max_length=20)
    city = models.CharField("市", max_length=20)
    district = models.CharField("县区", max_length=20)
    address = models.CharField("地址", max_length=255)
    status = models.IntegerField("状态", default=0)

    def __str__(self):
        return str(self.store)+"/"+self.user+"/"+str(self.mobile)

    class Meta:
        db_table = 'tmall_store'
        verbose_name_plural = u'天猫门店'
        permissions = (
            ("tmall_store", u"天猫门店"),
        )

class TmallStoreSn(models.Model):
    store = models.ForeignKey('TmallStore', on_delete=models.SET_NULL, null=True, verbose_name='门店')
    store_sn = models.CharField("门店编码", max_length=7)
    pid = models.CharField(max_length=255)
    click_num = models.IntegerField("状态", default=0)

    class Meta:
        db_table = 'tmall_store_sn'
        verbose_name_plural = u'天猫门店编号'
        permissions = (
            ("tmall_store_sn", u"天猫门店编号"),
        )